/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hyjiacan.deadqq.client.window;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.text.Document;

import org.hyjiacan.deadqq.client.i18n.I18N;
import org.hyjiacan.deadqq.common.entity.User;
import org.hyjiacan.deadqq.common.protocol.MessageProtocol;
import org.hyjiacan.deadqq.common.util.WindowUtil;

import com.hyjiacan.xx.client.util.JCapture;

/**
 * 聊天窗口，用户与其联系人通过此窗口进行交谈
 *
 * @author hyjiacan
 */
public class ChatWindow extends JFrame implements Runnable {
private Document msgDoc;
    /**
     *
     */
    private static final long serialVersionUID = -3596510831168484667L;
    // 聊天的对象（对方是谁）
    private User user;

    /**
     * 创建一个与User聊天的对话框
     *
     * @see User
     */
    public ChatWindow(User user) {
        this.user = user;
        // 聊天窗口的标题是聊天对象的昵称（Nickname）
        this.setTitle(user.getNickname());
       
    }

    @Override
    public void run() {
        initComponents();
        // 窗口居中显示
        WindowUtil.center(this);
        this.setVisible(true);
        WindowManager.put(this.getClass().getName() + "-" + user.getNumber(),
                this);
        
        msgDoc = message.getDocument();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        simpleProfile = new JPanel();
        messageHistoryScroller = new JScrollPane();
        messageHistory = new JEditorPane();
        toolBar = new JToolBar();
        textTool = new JButton();
        captureTool = new JButton();
        close = new JButton();
        send = new JButton();
        enterSend = new JCheckBox();
        messageScroller = new JScrollPane();
        message = new JEditorPane();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);
        setMinimumSize(new java.awt.Dimension(475, 426));

        simpleProfile.setBorder(new SoftBevelBorder(BevelBorder.RAISED));

        GroupLayout simpleProfileLayout = new GroupLayout(simpleProfile);
        simpleProfile.setLayout(simpleProfileLayout);
        simpleProfileLayout.setHorizontalGroup(
            simpleProfileLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        simpleProfileLayout.setVerticalGroup(
            simpleProfileLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 65, Short.MAX_VALUE)
        );

        messageHistory.setDragEnabled(true);
        messageHistory.setInheritsPopupMenu(true);
        messageHistoryScroller.setViewportView(messageHistory);

        toolBar.setFloatable(false);
        toolBar.setRollover(true);

        textTool.setText("T");
        textTool.setFocusable(false);
        textTool.setHorizontalTextPosition(SwingConstants.CENTER);
        textTool.setPreferredSize(new java.awt.Dimension(22, 22));
        textTool.setVerticalTextPosition(SwingConstants.BOTTOM);
        toolBar.add(textTool);

        captureTool.setText("CAP");
        captureTool.setFocusable(false);
        captureTool.setHorizontalTextPosition(SwingConstants.CENTER);
        captureTool.setVerticalTextPosition(SwingConstants.BOTTOM);
        captureTool.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                captureToolActionPerformed(evt);
            }
        });
        toolBar.add(captureTool);

        close.setText(I18N.getString("CLOSE")); // NO
        close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                closeActionPerformed(evt);
            }
        });

        send.setText(I18N.getString("SEND")); // NO
        send.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                sendActionPerformed(evt);
            }
        });

        enterSend.setSelected(true);
        enterSend.setText(I18N.getString("PRESS ENTER KEY TO SEND MESSAGE ?")); // NO
        enterSend.setToolTipText(I18N.getString("SEND MESSAGE WHEN ENTER KEY IS PRESSED.")); // NO

        messageScroller.setViewportView(message);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(simpleProfile, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(messageHistoryScroller)
            .addComponent(toolBar, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(enterSend)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(close)
                .addGap(21, 21, 21)
                .addComponent(send, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(messageScroller)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(simpleProfile, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(messageHistoryScroller, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(toolBar, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(messageScroller, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(close)
                    .addComponent(send)
                    .addComponent(enterSend)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void captureToolActionPerformed(ActionEvent evt) {//GEN-FIRST:event_captureToolActionPerformed
        JCapture.capture();
//        Transferable t = this.getToolkit().getSystemClipboard().getContents(null);
//        if(t!=null && t.isDataFlavorSupported(DataFlavor.imageFlavor)){
//            try {
//				message.insertIcon((Icon)t.getTransferData(DataFlavor.imageFlavor));
//			message.validate();
//			this.validate();
//            } catch (UnsupportedFlavorException | IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//        }

    }//GEN-LAST:event_captureToolActionPerformed

    private void sendActionPerformed(ActionEvent evt) {//GEN-FIRST:event_sendActionPerformed
       MessageProtocol msg = new MessageProtocol();
       
    }//GEN-LAST:event_sendActionPerformed

    private void closeActionPerformed(ActionEvent evt) {// GEN-FIRST:event_closeActionPerformed
        // 点击关闭的时候，是销毁聊天窗口而不是退出程序
        this.dispose();
    }// GEN-LAST:event_closeActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JButton captureTool;
    private JButton close;
    private JCheckBox enterSend;
    private JEditorPane message;
    private JEditorPane messageHistory;
    private JScrollPane messageHistoryScroller;
    private JScrollPane messageScroller;
    private JButton send;
    private JPanel simpleProfile;
    private JButton textTool;
    private JToolBar toolBar;
    // End of variables declaration//GEN-END:variables
}
